package com.goldgov.commylog.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.commylog.service.Commylog;
import com.goldgov.commylog.service.CommylogQuery;
import com.goldgov.commylog.service.CommylogService;
import com.goldgov.commylog.service.StatisticsCommylogBean;
import com.goldgov.commylog.service.StatisticsCommylogQuery;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench/commylog")
@Api("工作日志服务接口")
public class CommylogController {
	
	@Autowired
	private CommylogService service;
	
	@GetMapping("/preAddCommylog")
	@ApiOperation(value = "预新增工作日志", notes ="查询日志ID")
	public JsonSuccessObject<Commylog> preAddCommylog(Commylog obj,HttpServletRequest request){
		obj = service.getCommylogID();
		JsonSuccessObject<Commylog> returnResult = new JsonSuccessObject<>(obj);
		return returnResult;
	}
	
	@PostMapping
	@ApiOperation(value = "新增工作日志", notes ="新增工作日志")
	public JsonObject<Object> addCommylog(Commylog obj,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		obj.setUserID(u.getEntityID());
		obj.setUserName(u.getName());
		obj.setAuditState(CommylogQuery.AUDIT_STATE_ONE);
		obj.setActiveState(CommylogQuery.ACTIVE_STATE_YES);
		if(obj.getCreateDate()==null||obj.getCreateDate().equals("")) {
			obj.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		}
		service.addCommylog(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@DeleteMapping
	@ApiOperation(value = "批量工作日志删除", notes ="删除工作日志")
	public JsonObject<Object> deleteCommylog(@ApiParam(value = "commylogIDs", required = true) @RequestParam("ids") String[] ids){
		service.deleteCommylog(ids);
		return JsonSuccessObject.SUCCESS;
	}
	
	@PutMapping
	@ApiOperation(value = "工作日志更新", notes ="日志更新")
	public JsonObject<Object> updateCommylog(Commylog obj) {
		service.updateCommylog(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询工作日志", notes ="工作日志详情")
	public JsonSuccessObject<Commylog> getCommylog(@ApiParam(value="commylogID",required=false) @PathVariable("id") String id){
		Commylog obj = service.getCommylog(id);
		JsonSuccessObject<Commylog> returnResult = new JsonSuccessObject<>(obj);
		return returnResult;
	}
	
	@GetMapping("/")
	@ApiOperation(value = "查询多条日志", notes ="日志列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchAuditState", value = "日志状态查询条件", paramType = "query"),
		 @ApiImplicitParam(name = "searchCreateDateBegin", value = "日志查询开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchCreateDateEnd", value = "日志查询结束时间", paramType = "query")})
	public JsonQueryObject<Commylog> listCommylog(@ApiIgnore CommylogQuery query,HttpServletRequest request){
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		query.setSearchUserID(u.getSwbUserId());
		List<Commylog> list = service.listCommylog(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@PutMapping("/preUpdateCommylogByAdmin")
	@ApiOperation(value = "预更新", notes ="管理员查看信息时需要更新批阅状态")
	public JsonObject<Object> preUpdateCommylogByAdmin(Commylog obj,HttpServletRequest request) {
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		obj.setAuditUserID(u.getEntityID());
		obj.setAuditUserName(u.getName());
		obj.setAuditState(CommylogQuery.AUDIT_STATE_TWO);
		service.preUpdateCommylogByAdmin(obj);
		return JsonSuccessObject.SUCCESS;
	}
	/**
	 * @<>
	 * @author RongSL
	 * @version 1.0.0
	 *  */
	@GetMapping("/updateCommylogByAdmin")
	@ApiOperation(value = "日志审核", notes ="管理员对日志进行审核")
	public JsonObject<Object> updateCommylogByAdmin(Commylog obj,HttpServletRequest request) {
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		obj.setAuditUserID(u.getEntityID());
		obj.setAuditUserName(u.getName());
		obj.setAuditState(CommylogQuery.AUDIT_STATE_THREEE);
		service.updateCommylogByAdmin(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/listCommylogByAdmin")
	@ApiOperation(value = "管理员查询日志列表", notes ="供管理员使用,可以查询出直属当前管理员后台管理范围下的所有党员（包括管理员）")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchAuditState", value = "日志状态查询条件", paramType = "query"),
		 @ApiImplicitParam(name = "searchCreateDateBegin", value = "日志查询开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchCreateDateEnd", value = "日志查询结束时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchName", value = "人员姓名", paramType = "query"),
		 @ApiImplicitParam(name = "searchUserId", value = "人员id", paramType = "query"),
		 @ApiImplicitParam(name = "searchOrganizationId", value = "组织结构id", paramType = "query"),
	     @ApiImplicitParam(name = "searchTreePath", value = "组织结构treePath", paramType = "query")})
	public JsonQueryObject<Commylog> listCommylogByAdmin(@ApiIgnore CommylogQuery query,HttpServletRequest request){
//		String u=LoginAuthUser.getScopeCodeByFunctionID(AuthorityInfoInit.zbq,request);
//		query.setSearchScopeCode(u);
		try {
			List<Commylog> list = service.listCommylogByAdmin(query);
			query.setResultList(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new JsonQueryObject<>(query);
	}
	/**
	 * 查询机构人员
	 * @param query
	 * @param request
	 * @return
	 */
	@GetMapping("/findOrgUser")
	@ApiOperation(value = "查询机构人员", notes ="查询机构人员")
	public JsonQueryObject<Commylog> findOrgUser(CommylogQuery query,HttpServletRequest request){
		if(PropertyUtil.objectNotEmpty(query.getSearchNoUserId())) {
			query.setSearchNoUserIds(query.getSearchNoUserId().split(","));
		}
		List<Commylog> list = service.findOrgUser(query);
		for (Commylog commylog : list) {
			if(PropertyUtil.objectNotEmpty(commylog)&&(PropertyUtil.objectNotEmpty(commylog.getLoginName())&&commylog.getLoginName().length()==15||PropertyUtil.objectNotEmpty(commylog.getLoginName())&&commylog.getLoginName().length()==18)) {
				commylog.setLoginName(commylog.getLoginName().replaceAll("(\\d{4})\\d{10}(\\w{4})","$1*****$2"));
			}
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * 查询同机构人员
	 * @param query
	 * @param request
	 * @return
	 */
	@GetMapping("/findMyOrgUser")
	@ApiOperation(value = "查询同机构人员", notes ="查询同机构人员")
	public JsonQueryObject<Commylog> findMyOrgUser(CommylogQuery query,HttpServletRequest request){
		if(!PropertyUtil.objectNotEmpty(query.getSearchUserID())) {
			SwbAuthUser user=LoginAuthUser.getUserByAdmin(request);
			query.setSearchUserID(user.getEntityID());
		}
		List<Commylog> list = service.findOrgUser(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * 查询填写率
	 * @param query
	 * @param request
	 * @return
	 */
	@GetMapping("/statisticsTxl")
	@ApiOperation(value = "查询填写率", notes ="查询填写率")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "子组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationType", value = "机构类型", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationId", value = "机构ID", paramType = "query"),
		@ApiImplicitParam(name = "searchOrderType", value = "排序方式", paramType = "query"),
		@ApiImplicitParam(name = "searchTreePath", value = "机构tree", paramType = "query")
	})
	public JsonQueryObject<StatisticsCommylogBean> statisticsTxl(@ApiIgnore StatisticsCommylogQuery query,HttpServletRequest request){
		List<StatisticsCommylogBean> list = new ArrayList<StatisticsCommylogBean>();
		if (PropertyUtil.objectNotEmpty(query.getSerarchNextNum()) && query.getSerarchNextNum().intValue() > 0) {//包含下级组织是
			list = service.statisticsTxl(query);
		}else {
			list = service.statisticsTxlZB(query);
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * 查询审阅率
	 * @param query
	 * @param request
	 * @return
	 */
	@GetMapping("/statisticsSyl")
	@ApiOperation(value = "查询审阅率")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "子组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationId", value = "机构ID", paramType = "query")
	})
	public JsonQueryObject<StatisticsCommylogBean> statisticsSyl(@ApiIgnore StatisticsCommylogQuery query,HttpServletRequest request){
		List<StatisticsCommylogBean> list = new ArrayList<StatisticsCommylogBean>();
		if (PropertyUtil.objectNotEmpty(query.getSerarchNextNum()) && query.getSerarchNextNum().intValue() > 0) {//包含下级组织是
			list = service.statisticsSyl(query);
		}else {
			list = service.statisticsSylZB(query);
		}
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	/**
	 * 查询组织日志数量
	 * @param query
	 * @param request
	 * @return
	 */
	@GetMapping("/findOrgCommylogNum")
	@ApiOperation(value = "查询组织日志数量")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
		@ApiImplicitParam(name = "serarchNextNum", value = "子组织数量", paramType = "query"),
		@ApiImplicitParam(name = "searchOrderType", value = "排序类型", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationId", value = "机构ID", paramType = "query")
	})
	public JsonQueryObject<StatisticsCommylogBean> findOrgCommylogNum(@ApiIgnore StatisticsCommylogQuery query,HttpServletRequest request){
		List<StatisticsCommylogBean> list = service.findOrgCommylogNum(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
}